"""Prints the TPR and TNR coordinates to the screen, one point per line.

Orders the predictions and prints the TPR and TNR coordinates for each entry.
Usage:
    python calculate_TPR_TNR_plot.py predictions actualvalues 
"""
import sys
import getopt
import math

def process(predictions, actualvalues):
    """Prints the TPR and TNR coordinates to the screen, one point per line."""
    pfh = open(predictions, 'r')
    avfh = open(actualvalues, 'r')
    predictions = []

    for prediction in pfh:
        pred_type = ''
        actualvalue = avfh.readline().split()[0]

        p = float(prediction)
        av = int(actualvalue)
        predictions.append((p, av))

    pfh.close()
    avfh.close()

    sorted_pred = sorted(predictions, key=lambda prediction: prediction[0])

    tp = 0
    tn = 0
    fp = 0
    fn = 0

    print 'Sensitivity (TPR) Specificity (TNR)' 
    for (p, av) in sorted_pred:
        if p < 0:
            if av < 0:
                tn += 1
            else:
                fn += 1
        elif p > 0:
            if av > 0:
                tp += 1
            else:
                fp += 1

        if tp + fn != 0 and fp + tn != 0:
            print "%.2f %.2f" % (tp/float(tp+fn), tn/float(fp+tn))

def main():
    # parse command line options
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
    except getopt.error, msg:
        print msg
        print "for help use --help"
        sys.exit(2)
    # process options
    for o, a in opts:
        if o in ("-h", "--help"):
            print __doc__
            sys.exit(0)
    # process arguments
    if (len(args) == 2):
        process(args[0], args[1]) # process() is defined elsewhere
    else:
        print __doc__

if __name__ == "__main__":
    main()
